package wawi.kasse;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import wawi.actions.Artikel;
import wawi.actions.ArtikelDetails;
import wawi.actions.ArtikelUmbuchung;
import wawi.db.DbConnector;
import wawi.util.Laden;
import wawi.util.Props;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;



public class WarenumlaufListGUI extends JDialog {
	public WarenumlaufListGUI(Frame owner) {
		super(owner);
		initComponents();
	}

	public WarenumlaufListGUI(Dialog owner) {
		super(owner);
		initComponents();
	}

	private void fetchDataAusgang()
	{
		int filiale = Props.getInt("filiale", 0);				
		List<ArtikelUmbuchung> data = DbConnector.getInstance().getArtikelUmbuchungListVON(filiale);
		
		String[] names = new String[]{"Ziel", "Artikel", "Kombination", "Datum"};
		String[][] rows = new String[data.size()][4];
		int pos = 0;
		
		for (ArtikelUmbuchung au : data)
		{
			ArtikelDetails ad = au.getArtikelDetails();
			Artikel art = ad.getArtikel();
			
			rows[pos][0] = Laden.getName(au.getNach());
			rows[pos][1] = art.getTextKurz();
			rows[pos][2] = ad.getFarbe() + ", " + ad.getGroesse(); 
			rows[pos][3] = DateFormat.getDateInstance().format(au.getDatum_start()); 
			
			pos++;
		}
		
		ausgangTable.setModel(new DefaultTableModel(rows, names));
	}
	
	
	public void fetchData()
	{
		new Thread()
		{
			public void run()
			{
				fetchDataAusgang();
				fetchDataEingang();
			}
		}.start();
	}
	
	private void fetchDataEingang()
	{
		int filiale = Props.getInt("filiale", 0);
		List<ArtikelUmbuchung> data = DbConnector.getInstance()
				.getArtikelUmbuchungListNACH(filiale);

		String[] names = new String[] { "Herkunft", "Artikel", "Kombination", "Datum" };
		String[][] rows = new String[data.size()][4];
		int pos = 0;

		for (ArtikelUmbuchung au : data)
		{
			ArtikelDetails ad = au.getArtikelDetails();
			Artikel art = ad.getArtikel();

			rows[pos][0] = Laden.getName(au.getVon());
			rows[pos][1] = art.getTextKurz();
			rows[pos][2] = ad.getFarbe() + ", " + ad.getGroesse();
			rows[pos][3] = DateFormat.getDateInstance().format(
					au.getDatum_start());

			pos++;
		}

		eingangTable.setModel(new DefaultTableModel(rows, names));
	}
	
	
	private void okButtonActionPerformed(ActionEvent e) {
		setVisible(false);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		label1 = new JLabel();
		label2 = new JLabel();
		scrollPane1 = new JScrollPane();
		ausgangTable = new JTable();
		label3 = new JLabel();
		scrollPane2 = new JScrollPane();
		eingangTable = new JTable();
		buttonBar = new JPanel();
		okButton = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setTitle("Warenumlauf \u00dcbersicht");
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(Borders.DIALOG_BORDER);
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC
					},
					new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC
					}));

				//---- label1 ----
				label1.setText("Warenumlauf \u00dcbersicht");
				label1.setFont(new Font("Dialog", Font.BOLD, 16));
				contentPanel.add(label1, cc.xywh(1, 1, 5, 1));

				//---- label2 ----
				label2.setText("Ware im Ausgang");
				contentPanel.add(label2, cc.xywh(1, 5, 3, 1));

				//======== scrollPane1 ========
				{

					//---- ausgangTable ----
					ausgangTable.setPreferredScrollableViewportSize(new Dimension(450, 200));
					scrollPane1.setViewportView(ausgangTable);
				}
				contentPanel.add(scrollPane1, cc.xywh(1, 7, 5, 1));

				//---- label3 ----
				label3.setText("Erwartete Ware");
				contentPanel.add(label3, cc.xywh(1, 11, 3, 1));

				//======== scrollPane2 ========
				{

					//---- eingangTable ----
					eingangTable.setPreferredScrollableViewportSize(new Dimension(450, 200));
					scrollPane2.setViewportView(eingangTable);
				}
				contentPanel.add(scrollPane2, cc.xywh(1, 13, 5, 1));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
				buttonBar.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.GLUE_COLSPEC,
						FormFactory.BUTTON_COLSPEC
					},
					RowSpec.decodeSpecs("pref")));

				//---- okButton ----
				okButton.setText("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okButtonActionPerformed(e);
					}
				});
				buttonBar.add(okButton, cc.xy(2, 1));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JLabel label1;
	private JLabel label2;
	private JScrollPane scrollPane1;
	private JTable ausgangTable;
	private JLabel label3;
	private JScrollPane scrollPane2;
	private JTable eingangTable;
	private JPanel buttonBar;
	private JButton okButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
